//
//  YZGAllBrandModel.m
//  yzg
//
//  Created by EDS on 16/6/24.
//  Copyright © 2016年 AVGD. All rights reserved.
//

#import "LanguageHelper.h"



// 英语
NSString *const PreferredLanguageEN = @"en";
// 简体中文（中国大陆）
NSString *const PreferredLanguageZhHansCN = @"zh-Hans-CN";
// 繁体中文（中国香港）
NSString *const PreferredLanguageZhHantCN = @"zh-Hant-CN";
// 繁体中文（中国台湾）
NSString *const PreferredLanguageZhHantTW = @"zh-Hant-TW";
// 俄语
NSString *const PreferredLanguageRU = @"ru";
// 印尼语
NSString *const PreferredLanguageID = @"id";

@implementation LanguageHelper

/**
 单例
 */
+ (instancetype)shared{
    static LanguageHelper *instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[LanguageHelper alloc] init];
    });
    return instance;
}

+ (void)initSystemLanguage{
    
    //    "yue-Hans-CN",
    //    "zh-Hans-CN",
    //    "en-CN",
    //    "zh-Hant-HK",
    //    "zh-Hant-CN",
    //    "zh-Hant-TW",
    //    "zh-Hant-MO",
    //    "yue-Hant-CN"
    
    NSString *appLanguage = [MJUIUserDeFault objectForKey:MJ_AppLanguage];
    if(appLanguage.length){
        [LanguageHelper initLanguageStyle:appLanguage];
    }else{
        NSArray *languageArry = [NSLocale preferredLanguages];
        NSLog ( @"languageArry=================%@" , languageArry);
        
        NSString *language = [NSLocale preferredLanguages].firstObject;
        if([language hasPrefix:@"zh-"] || [language hasPrefix:@"yue-"]) {
            if ([language containsString:@"Hans"]) {
                language = PreferredLanguageZhHansCN; // 简体中文
            } else {
                if([language containsString:@"TW"]){
                    language = PreferredLanguageZhHantTW; // 台湾繁體
                }else{
                    language = PreferredLanguageZhHantCN; // 港澳繁體
                }
            }
        }else if([language hasPrefix:@"ru-"]){
            language = PreferredLanguageRU;
        }else if([language hasPrefix:@"id-"]){
            language = PreferredLanguageID;
        }else{
            language = PreferredLanguageEN;
        }
        //默认英文
    //    language = PreferredLanguageEN;
        
        [MJUIUserDeFault setObject:language forKey:MJ_AppLanguage];
        [LanguageHelper initLanguageStyle:language];
    }
    
}

+ (void)initLanguageStyle:(NSString *)language {
    NSLog(@"MJLog=============language==========%@",language);
    LanguageHelper.shared.language = language;

    MJLanguageStyle mLanguageStyle = MJLanguageStyleEN;
    if([language isEqualToString:PreferredLanguageEN]){
        mLanguageStyle = MJLanguageStyleEN;
    }
    if([language isEqualToString:PreferredLanguageZhHansCN]){
        mLanguageStyle = MJLanguageStyleZhHansCN;
    }
    if([language isEqualToString:PreferredLanguageZhHantCN]){
        mLanguageStyle = MJLanguageStyleZhHantCN;
    }
    if([language isEqualToString:PreferredLanguageZhHantTW]){
        mLanguageStyle = MJLanguageStyleZhHantTW;
    }
    if([language isEqualToString:PreferredLanguageRU]){
        mLanguageStyle = MJLanguageStyleRU;
    }
    if([language isEqualToString:PreferredLanguageID]){
        mLanguageStyle = MJLanguageStyleID;
    }
    LanguageHelper.shared.mLanguageStyle = mLanguageStyle;
}

+ (void)setCurrentLanguage:(NSString *)language {
    [MJUIUserDeFault setObject:language forKey:MJ_AppLanguage];
    [LanguageHelper initLanguageStyle:language];
}

- (NSString *)httpLanguage {
    //
    NSString *language = @"en-US";
    if(LanguageHelper.shared.mLanguageStyle == MJLanguageStyleEN){
        language = @"en-US";
    }
    if(LanguageHelper.shared.mLanguageStyle == MJLanguageStyleZhHansCN){
        language = @"zh-CN";
    }
    if(LanguageHelper.shared.mLanguageStyle == MJLanguageStyleZhHantCN){
        language = @"zh-HK";
    }
    if(LanguageHelper.shared.mLanguageStyle == MJLanguageStyleZhHantTW){
        language = @"zh-TW";
    }
    if(LanguageHelper.shared.mLanguageStyle == MJLanguageStyleRU){
        language = @"ru-RU";
    }
    if(LanguageHelper.shared.mLanguageStyle == MJLanguageStyleID){
        language = @"id-ID";
    }
//    NSLog(@"MJLog=======language================%@",language);

    return language;
}


@end
